package cn.edu.xjtu.android.emoji_warfare_android.game;

import java.util.Iterator;
import java.util.Random;

import cn.edu.xjtu.android.emoji_warfare_android.engine.worker.DelayedOperation;
import cn.edu.xjtu.android.emoji_warfare_android.game.hero.Boss;
import cn.edu.xjtu.android.emoji_warfare_android.game.hero.Enemy;
import cn.edu.xjtu.android.emoji_warfare_android.music.Musics;

/**
 * EnemyController<br>
 * 敌人控制器
 * 
 * @author wyyf
 *
 */
public class EnemyController extends DelayedOperation {
    Engine engine;
    EnemyLoader el;

    /**
     * @param engine
     *            引擎的引用
     * @param el
     *            敌人加载器
     */
    public EnemyController(Engine engine, EnemyLoader el) {
        this.setDelayMillis(3);
        this.engine = engine;
        this.el = el;
    }

    @Override
    public boolean run() {
        synchronized (el) {
            Iterator<Enemy> it = el.enemies.iterator();
            while (it.hasNext()) {
                Enemy en = it.next();
                if (en instanceof Boss) {
                    bossControl(it, en);
                } else if (en instanceof Enemy) {
                    enemyControl(it, en);
                }
            }
        }
        return false;
    }

    private void enemyControl(Iterator<Enemy> it, Enemy enemy) {
        if (enemy.status == 0) {
            // 第一阶段上场
            enemy.basePoint.y += 3.5f;
            if (enemy.basePoint.y >= 400) {
                enemy.status++;
            }
        } else if (enemy.status == 1) {

            // 第二阶段等待
            enemy.stayTimes += 3;
            if (enemy.stayTimes >= 1000 * 4) {
                enemy.status++;
            }
            int r = enemy.stayTimes % (1500);

            // 定时开火
            if (r < 3) {
                enemy.fire();
            }
        } else if (enemy.status == 2) {

            // 第三阶段退场
            enemy.basePoint.y += 3.5f;
        }

        // 敌人死亡
        if (enemy.hp <= 0) {

            calcDrop(enemy);
            Musics.getInstance().boom();
            enemy.basePoint.x = -1000;
            it.remove();
        }
    }

    private void calcDrop(Enemy enemy) {
        // 物品太多则不掉落
        if (Engine.instance.Layers.Item.size() > 5) {
            return;
        }
        // 物品掉落计算
        float p = mRandom.nextFloat();
        float ll;

        // 临近Boss提高激光掉率
        if (Engine.instance.el.waveIndex % 10 == 9) {
            ll = 0.33f;
        } else {
            ll = 0.80f;
        }
        if (Engine.instance.hasLaser) {
            ll = 1;
        }

        // 计算物品掉落
        if (p > 0.96) {
            enemy.dropItem(0);
        } else if (p > 0.92) {
            enemy.dropItem(1);
        } else if (p > 0.88) {
            enemy.dropItem(2);
        } else if (p > 0.83) {
            enemy.dropItem(4);
        } else if (p > ll) {
            enemy.dropItem(3);
            // 有激光(包括有这个物品和激光仍在启用)则不再掉落
            Engine.instance.hasLaser = true;
        }
    }

    private void bossControl(Iterator<Enemy> it, Enemy en) {
        Boss boss = (Boss) en;
        if (boss.status == 0) {
            // 第一阶段上场
            boss.basePoint.y += 1.5f;
            if (boss.basePoint.y >= 400) {
                boss.status++;
            }
        } else if (boss.status == 1) {

            // 第二阶段反复横跳
            boss.basePoint.x += boss.speedX;
            boss.stayTimes += 3;

            // 一定概率使用技能
            if (mRandom.nextDouble() < 0.005) {
                boss.status++;
            }
            if (boss.basePoint.x < 300 || boss.basePoint.x > 780) {
                boss.speedX = -boss.speedX;
            }

            // 单次开火
            if ((boss.stayTimes % 300) < 3) {
                boss.fire(6);
            }
        } else if (boss.status == 2) {

            // 批量开火
            int count = engine.el.waveIndex / 10 + 2;
            float[] xs = new float[count];
            for (int i = 0; i < count; i++) {
                xs[i] = 600f / (count - 1) * i - 300f;
            }
            boss.fire(10, xs);
            boss.status--;
        }

        // Boss死亡
        if (boss.hp < 0) {
            synchronized (Engine.instance.Layers.Enemy) {
                Engine.instance.Layers.Enemy.remove(boss);
            }
            it.remove();
            Engine.instance.el.isLoading = true;

        }
    }

    private static Random mRandom;

    static {
        mRandom = new Random(System.currentTimeMillis());
    }

}
